package com.demo.afternoon;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName 集合
 * @Description TODO
 * @Author tianqinglong
 * @Date 14:00 2022/7/8
 * @Version 1.0
 **/
public class 集合 {
    public static void main(String[] args) {
//        数组和ArrayList的互相转换();
//        练习();
        遍历();
    }

    private static void 遍历() {
        List<String> list = Arrays.asList("aaa", "bbb", "ccc");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+" ");
        }
        System.out.println();
        for (String string : list) {
            System.out.print(string+" ");
        }
        System.out.println();
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.print(next+" ");
        }
        System.out.println();
        list.stream().forEach(e-> System.out.print(e+" "));
        list.stream().forEach(System.out::println);
    }

    private static void 练习() {
        ArrayList<Float> list = new ArrayList<Float>();
        list.add(2.0f);
        list.add(3.1f);
        list.add(3.2f);
        System.out.println(list.indexOf(2.0f));
        list.set(list.indexOf(2.0f),6.0f);
      //  System.out.println(list.toString());
        list.remove(list.size()-1);
        System.out.println(list.size());
    }

    private static void 数组和ArrayList的互相转换() {
        Integer a[]={1,2,3};
        List<Integer> list = Arrays.asList(a);
        list.set(2,1000);
        // list.add(3);//报错

        Integer[] array = list.toArray(a);//转换为数组
        System.out.println(array[2]);

        System.out.println(list.get(2)+"    "+a[2]);
    }
}
